<?php
/**
 * Create By: PhpStorm
 * User: yihua
 * File: RemoveUserFavorite.php
 * Date: 2017/2/6
 * Time: 22:04
 */


namespace app\api\model\page\user;

use app\api\model\dao\UserFavorite;
use app\library\Error;
use app\library\PageBase;
use think\Log;

class RemoveUserFavorite extends PageBase
{
    private $objUserFavoriteModel;

    public function __construct()
    {
        parent::__construct();
        $this->objUserFavoriteModel = new UserFavorite();
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }
        $ret = $this->checkParam($param);
        if ($ret) {
            Log::error('param error: ' . json_encode($param));
            return $this->getRes(Error::ERR_PARAM, $ret);
        }
        $ret = $this->objUserFavoriteModel->removeFavorite($this->user_id, $param['id'], $param['fav_type']);
        if (false === $ret) {
            Log::error('get favorite failed' . json_encode($ret));
            return $this->getRes(Error::ERR_SYS, $ret);
        }
        if (0 == $ret) {
            return $this->getRes(Error::ERROR_REMOVE_FAVORITE_NOT_EXIST);
        }
        Log::info('remove favorite return ' . json_encode($ret));

        return $this->getRes();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'id' => 'require',
            'fav_type' => 'require',
        ];
        $msg = [
            'id.require' => '收藏ID必须',
            'fav_type.require' => '收藏类型必须',

        ];

        $validate = new \think\Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }
}


